package com.personal.service.Impl;

import com.personal.dao.ProductCategoryDao;
import com.personal.dao.ProductDao;
import com.personal.dto.ProductCategoryExecution;
import com.personal.entity.ProductCategory;
import com.personal.enums.ProductCategoryStateEnum;
import com.personal.exception.ProductCategoryOperationException;
import com.personal.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 萌新
 * @version 1.0
 * @date 2021/4/17 20:52
 */

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryDao productCategoryDao;

    @Autowired
    private ProductDao productDao;

    @Override
    public List<ProductCategory> getProductCategoryList(long shopId) {
        return productCategoryDao.queryProductCategoryList(shopId);
    }

    @Override
    public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException {
        //判断productCategoryList集合是否不为空或者这个集合长度是否大于0
        if (productCategoryList != null && productCategoryList.size() > 0){
            try {
                //集合里面有值
                int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
                if (effectedNum <= 0){
                    throw new ProductCategoryOperationException("店铺类别创建失败");
                }else {
                    return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
                }
            }catch (Exception e){
                throw new ProductCategoryOperationException("batchAddProductCategory error" + e.getMessage());
            }
        }else {
            //如果集合为空的话
            return new ProductCategoryExecution(ProductCategoryStateEnum.EMPETY_LIST);
        }
    }


    @Override
    @Transactional
    public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {
        //第一步 将此商品类别下的商品的类别id置为空
        try {
            int effectNum = productDao.updateProductCategory2Null(productCategoryId,shopId);
            if (effectNum < 0){
                throw new ProductCategoryOperationException("商品类别更新失败！");
            }
        }catch (Exception e){
            throw new ProductCategoryOperationException(e.getMessage());
        }

        //第二步 删除该商品目录
        try {
            int effectNum = productCategoryDao.deleteProductCategory(productCategoryId, shopId);
            if (effectNum > 0){
                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
            }else {
                return new ProductCategoryExecution(ProductCategoryStateEnum.INNER_ERROR);
            }
        }catch (Exception e){
            throw new ProductCategoryOperationException("deleteProductCategory error:"+e.getMessage());
        }
    }
}
